
package com.x52im.rbchat.logic.main.mainimpl;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.eva.android.widget.ActivityRoot;
import com.x52im.mall.MallGlobal;
import com.x52im.mall.logic.shop.OrderManageActivity;
import com.x52im.rbchat.R;

/**
 * 主界面中的"商城"子Fragment的标题栏封装根类。
 * <p>
 * 当前，主界面的UI组织方式是：单Activity+多个子Fragment基于ViewPager方式进行切换。而Fragment不像Activity，
 * 它是没有title栏的，为了子Fragment切换为当前界面界面时，能及时显示它对应的标题栏（包括标题文本、标题栏功能功能
 * 等），一个子Fragment将对应一个本类实现，其中将实现对主界面Activity的title栏的设置，从而显示本Fragment对应的
 * 各项内容（包括标题文本、标题栏功能等）。
 *
 * @author Jack Jiang
 * @since 8.0
 */
public class MallFragmentTitleWrapper extends AbstractFragmentTitleWrapper {

    private Button shopCar = null;
    private Button myOrder = null;
    private View shopCarCoutViewLayout = null;
    private TextView shopCarCountView = null;

    public MallFragmentTitleWrapper(ActivityRoot parentActivity) {
        super(parentActivity);
    }

    public void loadTitle() {
        resetTitle();

        myOrder = this.getTitleBar().getLeftGeneralButton(R.drawable.common_mall_shop_order_btn_selector);
        shopCar = this.getTitleBar().getRightGeneralButton(R.drawable.common_mall_shop_shopping_btn_selector);

        //* 将自定的购物车中商品数量子布局加入到标题栏右按钮区
        ViewGroup titleRightButtonsLayout = this.getTitleBar().findViewById(R.id.widget_title_rightBtnLayout);
        // 子布局
        shopCarCoutViewLayout = LayoutInflater.from(parentActivity).inflate(R.layout.common_mall_main_fragment_shop_car_count, titleRightButtonsLayout, false);
        // 子布局的商品数文本组件
        shopCarCountView = shopCarCoutViewLayout.findViewById(R.id.common_mall_main_layout_entrance_header_shop_car_countView);
        // 将子布局将入到标题栏右按钮区
        titleRightButtonsLayout.addView(shopCarCoutViewLayout);

        shopCar.setOnClickListener(v -> parentActivity.startActivity(com.x52im.mall.IntentFactory.createShopCarActivityIntent(parentActivity)));
        myOrder.setOnClickListener(v -> {
            // 当用户没用登陆时作未登陆的处理
            if (!MallGlobal.getMallProviderInstance(parentActivity).getRobotimeMall().isUserLogin())
                MallGlobal.getMallProviderInstance(parentActivity).getRobotimeMall().processForUserUnLogin(parentActivity);
            else {
                parentActivity.startActivity(new Intent(parentActivity, OrderManageActivity.class));
            }
        });

        // 刷新"商城"标题栏上的购物车内商量数量显示
        refreshShopCarCountShow();
    }

    public void clearShopCarCountView() {
        ViewGroup titleRightButtonsLayout = this.getTitleBar().findViewById(R.id.widget_title_rightBtnLayout);
        if (shopCarCoutViewLayout != null)
            titleRightButtonsLayout.removeView(shopCarCoutViewLayout);
    }

    public void refreshShopCarCountShow() {
        int shopCarCount = MallGlobal.getMallProviderInstance(parentActivity).getRobotimeMall().getShopCarProvider().getGoodsCount();
        if (shopCarCount == 0) {
            if (shopCarCountView != null)
                shopCarCountView.setVisibility(View.GONE);
        } else {
            if (shopCarCountView != null) {
                shopCarCountView.setVisibility(View.VISIBLE);
                shopCarCountView.setText(shopCarCount + "");
            }
        }
    }
}